<template>
  <Approval :nodeConfig="approvalTree" @addNode="addNode" @del-node="delNode" />
  <ConditionDrawer :conditionForm="props.conditionForm" @change="getData" />
  <ApproverDrawer />
  <CopyerDrawer />
  <PromoterDrawer @change="getData" />
  <!-- <a-space class="btn">
    <a-button @click="saveFlow" :type="'primary'">保存流程</a-button>
  </a-space> -->
</template>
<script setup lang="ts">
import Approval from '@/components/approval/index.vue';
import ConditionDrawer from './componnents/ConditionDrawer.vue';
import ApproverDrawer from './componnents/ApproverDrawer.vue';
import CopyerDrawer from './componnents/CopyerDrawer.vue';
import PromoterDrawer from './componnents/PromoterDrawer.vue';
import {
  AddFlowPanelPoint,
  getFlowPanelPointList,
  delFlowPanelPointByIds,
  editFlowPanelPoint
} from '@/apis/flowPanelPoint/index';
import type { FlowPanelPoint, FlowTree } from '@/apis/flowPanelPoint/type';
import array2approvalTree from '@/hooks/approval/index';
import { useRoute, useRouter } from 'vue-router';
const route = useRoute();
const router = useRouter();

const props = withDefaults(
  //参数说明
  defineProps<{
    showTemplate: boolean;
    conditionForm: any;
  }>(),
  {
    showTemplate: false
  }
);
const emits = defineEmits(['save']);
const processId = ref();
const init = () => {
  let id = Number(router.currentRoute.value.query.id);
  if (id > 0) {
    processId.value = id;
    getData();
  }
};
onMounted(() => {
  init();
});
const addNode = async (data: FlowPanelPoint) => {
  await AddFlowPanelPoint(data);
  getData();
};
//删除流程节点
const delNode = async (conditionId: number, changePoints: FlowPanelPoint[]) => {
  //changePoints不对
  console.log('conditionId', conditionId, changePoints);

  // await delFlowPanelPointByIds({ ids: [conditionId] });
  // 删除节点后，改变大于此节点的优先级（全部减一）
  // for await (const point of changePoints) {
  //   const { id, priority } = point;
  //   editFlowPanelPoint({ id, priority: priority - 1 });
  // }

  getData();
};
const panelPointList = ref([]);
const approvalTree = ref<FlowTree>();
const getData = async () => {
  const res = await getFlowPanelPointList({ processId: processId.value });
  panelPointList.value = res?.panelPointList || [];
  const { gg } = array2approvalTree(res?.panelPointList);
  const treeData = gg(res?.panelPointList[0]);
  approvalTree.value = treeData;
  console.log('treeData', treeData);
};
</script>
<style lang="less" scoped>
.btn {
  position: fixed;
  top: 14px;
  right: 16px;
}
</style>
